package utils;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;

public class Time {
    public static  String getNowTime(){
        // 获取当前时间戳
        Timestamp b_time = new Timestamp(System.currentTimeMillis());
        // 定义日期格式化模板
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 格式化时间戳为指定格式的日期字符串
        return dateFormat.format(new Date(b_time.getTime()));
    }
    public static String getLimitTime(){
        // 获取当前时间戳
        Timestamp n_time = new Timestamp(System.currentTimeMillis());
        // 转换为 Calendar 对象
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(n_time.getTime());
        // 添加3个月
        cal.add(Calendar.MONTH, 3);
        // 获取期限时间
        Timestamp l_time = new Timestamp(cal.getTimeInMillis());
        // 定义日期格式化模板
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 格式化时间戳为指定格式的日期字符串
        return dateFormat.format(new Date(l_time.getTime()));
    }
    public static int days(String rtime,String ltime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.parse(rtime, formatter);
        LocalDate date2 = LocalDate.parse(ltime, formatter);
        long daysDiff = ChronoUnit.DAYS.between(date1, date2);
//        return Math.abs((int) daysDiff);
        return (int) daysDiff;
    }

    public static void main(String[] args) {
        System.out.println(days("2024-12-01","2024-10-01"));
    }
}
